package com.ssm.zy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.zy.mapper.FollowMapper;
import com.ssm.zy.mapper.LabelMapper;
import com.ssm.zy.mapper.PostMapper;
import com.ssm.zy.mapper.UserMapper;
import com.ssm.zy.pojo.Post;
import com.ssm.zy.pojo.User;
import com.ssm.zy.pojo.vo.PostVo;
import com.ssm.zy.service.AdministratorsService;
import com.ssm.zy.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import java.util.Date;

/**
 * @author 柳艾迪~
 * @version 1.0
 */
@Service
public class AdministratorsServiceImpl implements AdministratorsService {
  @Autowired
  private PostMapper postMapper;
  @Autowired
  private FollowMapper followMapper;
  @Autowired
  private LabelMapper labelMapper;
  @Autowired
  private UserMapper userMapper;
  @Override
  public int examine(Integer postId, int judge) {
    int count = 0;
    Date date = new Date();
    if (judge==0) {
      count=postMapper.trueExaminePost(postId,date);
    }else{
      count=postMapper.falseExaminePost(postId,date);
    }
    return count;
  }

  @Override
  public int deletePost(Integer postId) {
    return postMapper.deletePost(postId);
  }

  @Override
  public PageInfo<PostVo> checkExamine(int userId ,int pageNum,int pageSize) { //查看需要审核的帖子

    PageHelper.startPage(pageNum,pageSize);
    List<PostVo> ExamineList =  setPostVoList( postMapper.checkExamine(),userId);
    return new PageInfo<>(ExamineList);
  }

  @Override
  public List<PostVo> findAllPost(int userId) {
    return setPostVoList( postMapper.findAllPost(),userId);//  查找所有帖子
  }

  @Override
  public PageInfo<User> findAllUser(int pageNum, int pageSize) {//查找所有用户
    PageHelper.startPage(pageNum,pageSize);
    List<User> userList=userMapper.findAllUser();
    return new PageInfo<>(userList);
  }

  private List<PostVo> setPostVoList(List<PostVo> postVoList,Integer userId) {
    for (PostVo postVo : postVoList) {
      //判断是否关注
      postVo.setIsFollow(followMapper.getPassiveFocusId(
              postVo.getUserId()).contains(userId) || userId == postVo.getUserId());
      //判断是否点赞
      postVo.setIsPraise(postMapper.getPraiseOfUser(
              postVo.getPostId()).contains(userId));
      //判断是否收藏
      postVo.setIsCollect(postMapper.getCollectOfUser(
              postVo.getPostId()).contains(userId));
      //获取标签
      postVo.setLabelList(labelMapper.getLabelById(postVo.getPostId()));
    }
    return postVoList;
  }


}
